package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.timothy.android.BaseProcessActivity;
import com.timothy.android.R;
import com.timothy.android.view.BaseBean;
import com.timothy.service.MessageSenderUtil;
import com.timothy.statistics.AnswerBuilder;

public abstract class BaseChartActivity extends BaseProcessActivity implements OnDoubleTapListener, OnGestureListener,OnTouchListener{
	
	private final static String LOG_TAG = "BaseChartActivity";
	
	//set header
	protected TextView priorityList = null;
	
	//set BaseBean and BaseBeanList
	protected BaseBean baseBean = new BaseBean();
	protected List<BaseBean> baseBeanList = new ArrayList<BaseBean>();//only for priority
	protected void setBaseBean(String message,String type){
		switch(Integer.valueOf(type)){
			case 42:baseBean = AnswerBuilder.parseBaseBeanForPriority(message,"1");
			baseBeanList = AnswerBuilder.parseBaseBeanListForPriority(message);break;
			case 45:baseBean = AnswerBuilder.parseBaseBeanForGrading(message);break;
			default:baseBean = AnswerBuilder.parseBaseBean(message);
		}
		
	}
	
	protected PopupWindow popupWindow;
	protected ListView lv_group;
	protected List<String> groups;
	protected Integer pressedItemIndex = -1;
	protected View popupView;
	
	protected void showWindow(View parent) {
		Log.i(LOG_TAG, "showWindow()...");
		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			popupView = layoutInflater.inflate(R.layout.group_list, null);
			lv_group = (ListView) popupView.findViewById(R.id.lvGroup);
			groups = new ArrayList<String>();
			
			for(int i=0;i<baseBeanList.size();i++){
				groups.add("Priority " + String.valueOf(i+1));
			}
			
			Log.i(LOG_TAG, "pressedItemIndex:"+pressedItemIndex);
			GroupAdapter groupAdapter = new GroupAdapter(this, groups);
			lv_group.setAdapter(groupAdapter);
			popupWindow = new PopupWindow(popupView, 250, 400);
		}else{
			popupWindow.dismiss();
		}

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);//window will dismiss once touch out of it
		popupWindow.setBackgroundDrawable(new BitmapDrawable());//window will dismiss once click back 
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//		int xPos = windowManager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth() / 2;
		int xPos = windowManager.getDefaultDisplay().getWidth() / 3 ;
		Log.i(LOG_TAG, "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 0);
		lv_group.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				Log.i(LOG_TAG, "onItemClick()...");
				//initial all items background color
        		for(int i=0;i<adapterView.getChildCount();i++){
        			View item = adapterView.getChildAt(i);
        			item.setBackgroundColor(Color.parseColor("#4C4C4C"));
        		}
				view.setBackgroundColor(Color.parseColor("#D5E43C"));
				pressedItemIndex = position;
				Log.i(LOG_TAG, "pressedItemIndex:" + pressedItemIndex);
				if (popupWindow != null) {
					popupWindow.dismiss();
				}
				priorityList.setText("Priority "+String.valueOf(position+1));
				changePriority(position);
				Log.i(LOG_TAG, "onItemClick() END");
			}
		});
	}
	
	public abstract void setHeader(Integer type);
	public abstract void changePriority(int priority);
	
//	public abstract void loadNextButton();
	
	//send answers to server
	public void sendNextQuestion(){
		Log.i(LOG_TAG, "sendNextQuestion()...");
		
		String message = MessageSenderUtil.sendNextQuestion();
		Log.i(LOG_TAG, "message:"+message);
		
		if(!sendMessage(message)){
			ShowDialog("Can not connect to pc, please confirm PC is running!");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Bundle bundle = this.getIntent().getExtras();
		//get message and set BaseBean
		String message = bundle.getString("message");
		String type = bundle.getString("type");
		setBaseBean(message,type);
		
//		loadNextButton();
	}
	
	@Override
	protected void processMessage(int what, String content) {
		// TODO Auto-generated method stub
		super.processMessage(what, content);
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	protected int verticalMinDistance = 5;
	protected int horizontalMinDistance = 5;
    protected int minVelocity         = 0;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

    
}
